<?php
/**
 * 优惠券控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Coupon extends \Controller_BaseController {

	public $template = 'edu/template';

	public function action_index(){
		$params = array(
  			'title' => '优惠券管理',
  			'menu' => ''
  		);

		$params['items'] = \Model_Coupon::getItems('*', null, array('records'));
		$params['levels'] = \Model_MemberLevel::getItems();
		$params['members'] = \Model_Member::getItems('*');

  		\View::set_global($params);
		$this->template->content = \View::forge("edu/member/coupons");
	}

	/**
	* 查看一个优惠券
	*
	* @return
	*/
	public function action_view($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$coupon = \Model_Coupon::find($id);
		if( ! $coupon){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		if(\Input::is_ajax()){
			$coupon->start = date('Y-m-d H:i:s', $coupon->start);
			$coupon->end = date('Y-m-d H:i:s', $coupon->end);
			die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $coupon->to_array(), 'url' => \Config::get('base_url'))));
		}
	}

	/**
	* 新增一个优惠券
	*
	* @return
	*/
	public function action_create(){
		if(\Input::method() == 'POST'){

			$data = \Input::post();
			$data['is_delete'] = 0;

			$data['start'] = strtotime($data['start']);
			$data['end'] = strtotime($data['end']);

			$coupon = \Model_Coupon::forge($data);

			if($coupon->save()){
				\Response::redirect('/admin/coupon');
			}
		}
	}

	/**
	* 编辑一个优惠券
	*
	* @param $id int 优惠券ID
	* @return
	*/
	public function action_edit($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$coupon = \Model_Coupon::find($id);
		if( ! $coupon){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$coupon->set(\Input::post());
		if($coupon->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
			\Response::redirect('/admin/coupon');
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

	/**
	* 删除一个优惠券
	*
	* @param $id int 优惠券ID
	* @return
	*/
	public function action_delete($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$coupon = \Model_Coupon::find($id);
		if( ! $coupon){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$coupon->is_delete = 1;
		if($coupon->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

	/**
	* 赠送优惠券
	* @param $type 被赠送对象 member:会员 group:会员组
	*/
	public function action_give($type = 'member'){
		if(\Input::method() == 'POST'){
			$data = \Input::post();
			$coupon = \Model_Coupon::find($data['coupon_id']);
			$count = \Model_UserCoupon::query()
						->where('coupon_id', $data['coupon_id'])
						->count();

			if($count >= $coupon->count){
				die(json_encode(array('status' => 'err', 'msg' => '没有可用优惠券，赠送失败', 'errcode' => 11)));
			}

			if($type == 'group'){
				$members = \Model_Member::query()->where('level_id', $data['group_id']);
				$total = $members->count();

				if($total > ($coupon->count - $count)){
					die(json_encode(array('status' => 'err', 'msg' => "优惠券不足，该级别会员有{$total}个，优惠券剩余" . ($coupon->count - $count) . '张', 'errcode' => 12)));
				}
				foreach ($members->get() as $key => $value) {
					$item = array(
						'coupon_id' => $data['coupon_id'],
						'user_id' => $value->user_id,
						'status' => 'NONE'
					);
					\Model_UserCoupon::forge($item)->save();
				}
			}else if($type == 'member'){
				$data['status'] = 'NONE';
				$entity = \Model_UserCoupon::forge($data);
				if( ! $entity->save()){
					die(json_encode(array('status' => 'err', 'msg' => "优惠券赠送失败", 'errcode' => 20)));
				}
			}
			die(json_encode(array('status' => 'succ', 'msg' => "优惠券赠送完成", 'errcode' => 0)));
		}
	}


}
